package com.codergd.gra.service;

import com.codergd.gra.bean.Com;
import com.codergd.gra.bean.User;
import com.codergd.gra.config.PageQuery;
import com.codergd.gra.config.PageResult;
import com.codergd.gra.dao.ComDao;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ComService {
    @Autowired
    ComDao comDao;

    public List<Com> selectCom1(String comname){
        return comDao.selectCom1(comname);
    }

    public int isLogin(String iphone, String password){
        //用户不存在
        if (comDao.selectCom(iphone) == null){
            return -1;
            //用户密码错误
        }else if(!comDao.selectCom(iphone).equals(password)){
            return 0;
        }else if (comDao.selectCom3(iphone) == 0){
            return 2;
        }
        //密码正确
        return 1;
    }

    public int registered(Com com){
        //用户已存在
        if (isLogin(com.iphone,com.password) != -1){
            return 0;
        }
        //数据插入成功
        if(comDao.insertCom(com) != -1) {
            return 1;
        }
        return -1;
    }

    //根据手机号查找id
    public int selectId(String iphone){
        return comDao.selectId(iphone);
    }

    //根据id查找企业信息
    public List<Com> selectComById(int id){
        return comDao.selectComById(id);
    }

    public List<Com> selectCom2(){
        return comDao.selectCom2();
    }

    //修改企业状态
    public int updateState(int id,int state){
        return comDao.updateState(id,state);
    }
    //修改企业信息
    public int updateCom(int id,String iphone,String comname,String password,String address,String manage,String introduce){
        return comDao.updateCom(id,iphone,comname,password,address,manage,introduce);
    }

    //分页查询人才
    public PageResult pageQuery(PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(),pageQuery.getPageSize());
        Page<User> page = comDao.pageQuery(pageQuery);
        long total = page.getTotal();
        List<User> records = page.getResult();
        return new PageResult(total,records);
    }
}
